package com.linlei.bluetoothchat;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by LinLei on 2015/3/21.
 */
public class ChatListAdapter extends BaseAdapter {

    public LayoutInflater mInflater;
    public ArrayList<ChatActivity.SiriListItem> list;

    public ChatListAdapter(Context context, ArrayList<ChatActivity.SiriListItem> list2) {
        list = list2;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        ChatActivity.SiriListItem item = list.get(position);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder((TextView) convertView.findViewById(R.id.chat_msg));
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (item.isSiri) {
            holder.msg.setBackgroundResource(R.drawable.bg_rec);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            holder.msg.setLayoutParams(lp);
        } else {
            holder.msg.setBackgroundResource(R.drawable.bg_send);
            holder.msg.setTextColor(Color.WHITE);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            holder.msg.setLayoutParams(lp);

        }
        holder.msg.setText(item.message);
        return convertView;
    }

    class ViewHolder {
        protected TextView msg;

        public ViewHolder(TextView msg) {
            this.msg = msg;
        }


    }
}

